#!/bin/bash
if [ "$1" == "clean" ]; then
    echo "clean all Cmake and Bin"

    # 删除所有生成物
    make clean

    # 删掉所有CMake产物
    find . -name "CMakeCache.txt" -type f -delete
    find . -name "cmake_install.cmake" -type f -delete
    # find . -name "Makefile" -type f -delete
    find . -name "CMakeFiles" -type d -exec rm -rf {} +

    rm BuiltBin/* -rf

    exit 0
fi

# 保存当前目录
RootDir=$(pwd)

# 先执行Log/Client下的Makefile，得到LibClient.a，因为很多cmake需要关联它，否则cmake会出错
if [ -f "Log/Client/CMakeLists.txt" ]; then
    echo "Executing Makefile in Log/Client---------------------"
    cd Log/Client 
    cmake .
    make
    sleep 1
else
    echo "Makefile not found in Log/Client"
fi


# 回到项目根目录
cd "$RootDir"

# 获取Git信息
GIT_VERSION_DATA=$(git rev-parse HEAD)
GIT_BRANCH_DATA=$(git symbolic-ref --short HEAD)

VERSION_TITLE="#define GIT_VERSION"
BRANCH_TITLE="#define GIT_BRANCH"

GIT_VERSION_ALL_DATA="${VERSION_TITLE} \"${GIT_VERSION_DATA}"
GIT_BRANCH_ALL_DATA="${BRANCH_TITLE} \"${GIT_BRANCH_DATA}"


sed -i "/^${VERSION_TITLE}/c${GIT_VERSION_ALL_DATA}\"" SystemTools/SysInfo.h
sed -i "/^${BRANCH_TITLE}/c${GIT_BRANCH_ALL_DATA}\"" SystemTools/SysInfo.h

cmake .

# 执行根目录下的 Makefile
if [ -f "Makefile" ]; then
    echo "Executing Makefile in root directory"
    make -j32
else
    echo "Makefile not found in root directory"
fi


# 把需要用的拷贝到BuildBin中
mv Log/Server/Log_Server BuiltBin
mv ArmPolaris BuiltBin